﻿#region  文件信息

// ---------------------------------------------------------------------------------------
// <copyright file="Upload.cs" company="">
// 作者：李帅伦
// </copyright>
// <summary>
// 类型简介：
// 类型详细：Upload.cs
// 创建时间：2015-03-23 14:27
// 修改时间：2015-01-04 20:52
// </summary>
// ---------------------------------------------------------------------------------------
#endregion

using WebCommon.Com;

namespace WebCommon.Up
{
    #region

    using System;
    using System.IO;
    using System.Linq;
    using System.Web;

    using WebCommon.IO;

    #endregion

    /// <summary>
    /// The upload file.
    /// </summary>
    public class UploadFile
    {
        #region Fields

        /// <summary>
        /// The hb.
        /// </summary>
        protected HttpPostedFileBase Hb;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="UploadFile"/> class.
        /// </summary>
        /// <param name="hb">
        /// The hb.
        /// </param>
        public UploadFile(HttpPostedFileBase hb, string rootpath)
        {
            
            this.Hb = hb;
            var dts = DateTime.Now.ToString("yyyyMMdd");
            this. SaRootPath=  "upload/file/" + dts + "/";
            this.SaFilePath = rootpath + SaRootPath; 
            System.IO.Directory.CreateDirectory(this.SaFilePath);

            this.SaFileName = RandomsAzNum.GetLn(16);
        }

        #endregion

        #region Public Properties

        /// <summary>
        ///    获取扩展名
        /// </summary>
        public string GetExtension
        {
            get
            {
                var extension = Path.GetExtension(this.Hb.FileName);
                if (extension != null) {return extension.ToLower();}
                else
                {
                    return "";
                }
            }
        }

        /// <summary>
        /// 大小Kb
        /// </summary>
        public int GetSize
        {
            get { return this.Hb.ContentLength / 1024; }
        }

        /// <summary>
        /// 根目录
        /// </summary>
        public string SaRootPath { get; set; }

        /// <summary>
        /// 异常消息
        /// </summary>
        public string Ms { get; protected set; }



        /// <summary>
        ///     设置保存的文件名
        /// </summary>
        public string SaFileName { get; set; }

        /// <summary>
        ///     保存设置路径
        /// </summary>
        protected string SaFilePath { get; set; }

        /// <summary>
        ///     保存后的完整路径
        /// </summary>
        public string SaPath { get; protected set; }

        /// <summary>
        ///     上传类型集合
        /// </summary>
        public string[] TypeList { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// 获取二进制数组
        /// </summary>
        /// <returns>
        /// The <see cref="byte[]"/>.
        /// </returns>
        public byte[] Getbyte()
        {
            var inputStream = this.Hb.InputStream;
            byte[] bytes = new byte[inputStream.Length];
            inputStream.Write(bytes, 0, bytes.Length);
            return bytes;
        }

        /// <summary>
        /// 保存文件
        /// </summary>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        public virtual bool Save()
        {
            var p = this.SaFilePath + this.SaFileName + this.GetExtension;
            var save = this.Save(p);
            if (save)
            {
                this.SaPath = p;
            }

            return save;
        }

        #endregion

        #region Methods

        /// <summary>
        /// 是否通过类型
        /// </summary>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        public bool IsType()
        {

            if (this.TypeList != null)
            {
                return this.TypeList.Any(x => this.GetExtension == x);
                 
            }
            return true;
        }

        /// <summary>
        /// The save.
        /// </summary>
        /// <param name="path">
        /// The path.
        /// </param>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        protected bool Save(string path)
        {

            if (this.IsType())
            {
                if (this.Hb == null)
                {
                    this.Ms = "Hb为空";
                    return false;
                }
                this.Hb.SaveAs(path);
                return true;
            }
            else
            {
                this.Ms = "文件类型验证失败";
                return false;
            }

     

        }

        #endregion
    }
}